<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\common\model\bill;

use app\common\model\BaseModel;
use app\common\model\property\basic\PropertyBasicModel;
use app\common\model\users\UsersModel;
use app\common\model\room\RoomBasicModel;

class BillBasicModel extends BaseModel
{
    protected $autoWriteTimestamp = 'datetime';
    protected $createTime = 'add_time';
    protected $updateTime = false;

    public static function tablePk(): string
    {
        return 'id';
    }

    public static function tableName(): string
    {
        return 'bill_basic';
    }

    public function propertyInfo()
    {
        return $this->hasOne(PropertyBasicModel::class, 'id', 'property_id');
    }

    public function roomInfo()
    {
        return $this->hasOne(RoomBasicModel::class, 'id', 'room_id');
    }

    public function userInfo()
    {
        return $this->hasOne(UsersModel::class, 'id', 'user_id');
    }

    /**
     * 电费总额
     * @return float
     *
     * @author Amos
     * 2024-02-08 19:48
     */
    public function getElectricAmountAttr()
    {
        return 0.00;
    }

    /**
     * 冷水费金额
     * @return float
     *
     * @author Amos
     * 2024-02-08 19:51
     */
    public function getColdWaterAmountAttr()
    {
        return 0.00;
    }

    /**
     * 热水费金额
     * @return float
     *
     * @author Amos
     * 2024-02-08 19:51
     */
    public function getHotWaterAmountAttr()
    {
        return 0.00;
    }

    /**
     * 燃气费用金额
     * @return float
     *
     * @author Amos
     * 2024-02-08 19:51
     */
    public function getGasAmountAttr()
    {
        return 0.00;
    }


    /**
     * 其它费用金额
     * @return float
     *
     * @author Amos
     * 2024-02-08 19:51
     */
    public function getOtherAmountAttr()
    {
        return 0.00;
    }

    /**
     *  应收账单总额
     * @return float
     *
     * @author Amos
     * 2024-02-08 19:46
     */
    public function getArAmountAttr()
    {
        return BillCatalogModel::where('bill_id', $this->id)
            ->sum('total_amount');
    }

    /**
     *  实收账单总额
     * @return float
     *
     * @author Amos
     * 2024-02-08 19:46
     */
    public function getTotalAmountAttr()
    {
        # 实收 =  应收 - 优惠费用金额
        return bcsub($this->ar_amount, $this->minus_amount, 2);
    }

    /**
     * 滞纳金金额
     * @return float
     *
     * @author Amos
     * 2024-02-08 22:15
     */
    public function getOverdueFineAttr()
    {
        return BillCatalogModel::where('bill_id', $this->id)
            ->sum('overdue_fine');
    }


    /**
     *  优惠费用金额
     * @return float
     *
     * @author Amos
     * 2024-02-08 19:46
     */
    public function getMinusAmountAttr()
    {
        return BillCatalogModel::where('bill_id', $this->id)
            ->sum('minus_amount');
    }


    /**
     * 待确认金额
     * @return float
     *
     * @author Amos
     * 2024-02-08 19:45
     */
    public function getStayCheckAmountAttr()
    {
        return BillCourseModel::where('bill_id', $this->id)
            ->where('check_type', 1)
            ->sum('pay_amount');
    }


    /**
     * 线上支付金额
     * @return float
     *
     * @author Amos
     * 2024-02-08 19:45
     */
    public function getOnlineAmountAttr()
    {
        return BillFinanceModel::where('bill_id', $this->id)
            ->where('pay_type', 1)
            ->sum('amount');
    }

    /**
     * 线下支付金额
     * @return float
     *
     * @author Amos
     * 2024-02-08 19:45
     */
    public function getOfflineAmountAttr()
    {
        return BillFinanceModel::where('bill_id', $this->id)
            ->where('pay_type', 2)
            ->sum('amount');
    }


    /**
     * 己确认收款金额
     * @return string
     *
     * @author Amos
     * 2024-02-08 19:45
     */
    public function getCheckAmountAttr()
    {
        return bcadd($this->online_amount, $this->offline_amount, 2);
    }


    /**
     * 待支付金额
     * @return string
     *
     * @author Amos
     * 2024-02-08 19:45
     */
    public function getStayPaymentAmountAttr()
    {
        # 待支付 =  实收账单总额 - 待确认金额 - 己确认收款
        return bcsub(bcsub($this->total_amount, $this->stay_check_amount, 2), $this->check_amount, 2);
    }


}
